package com.sunshine.lnuplus.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.LayoutInflater
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.adapter.LoadSchoolAdapter
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.callback.LoadQingGuoSchoolListCallBack
import com.sunshine.lnuplus.internet.LoadQingGuoLesson
import com.sunshine.lnuplus.model.QingGuoSchoolBean
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.utils.TagHelper
import com.sunshine.lnuplus.view.DialogWithView
import com.sunshine.lnuplus.viewModel.LoginViewModel
import kotlinx.android.synthetic.main.activity_load_file_lesson_choose.*

class LoadSchoolLessonChooseActivity : BaseActivity() {

    private lateinit var myAdapter: LoadSchoolAdapter
    private lateinit var loginViewModel: LoginViewModel
    private val schoolList = ArrayList<ArrayList<String>>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_load_file_lesson_choose)

        loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        loadQingGuoSchoolList()
    }

    private fun initView() {
        title = "选择院校"
        StatusBarUtils.setStatusBarWithColor(this, color)
        myAdapter = LoadSchoolAdapter(this, schoolList)

        load_recycler.apply {
            layoutManager = LinearLayoutManager(this@LoadSchoolLessonChooseActivity)
            addItemDecoration(DividerItemDecoration(this@LoadSchoolLessonChooseActivity, DividerItemDecoration.VERTICAL))
            adapter = myAdapter
        }

        sidebar.setOnTouchLetterChangeListener {
            val pos = myAdapter.getIndex(it)
            if(pos != -1){
                load_recycler.scrollToPosition(pos)
                val layoutManager = load_recycler.layoutManager as LinearLayoutManager
                layoutManager.scrollToPositionWithOffset(pos, 0)
            }
        }

        getToolBar()?.setNavigationOnClickListener {
            supportFinishAfterTransition()
        }

    }

    private fun loadQingGuoSchoolList(){
        val dialog = DialogWithView.newInstance(
            LayoutInflater.from(this).inflate(R.layout.dialog_loading, null, false),
            "请稍后",
            false,
            null,
            null
        )
        dialog.show(supportFragmentManager, "")

        LoadQingGuoLesson.loadSchoolList(object : LoadQingGuoSchoolListCallBack {
            override fun onSuccess(schoolList2: List<QingGuoSchoolBean>) {
                schoolList2.forEach {
                    val temp = ArrayList<String>()
                    temp.apply {
                        add(it.xxmc)
                        add(it.pinyin)
                        add(it.xxdm)
                        add("QG")
                    }
                    schoolList.add(temp)
                }
                dialog.dismiss()
                handler.sendEmptyMessage(1)
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == TagHelper.LOADSCHOOLCHOOSE && resultCode == TagHelper.LOADSCHOOL){
            setResult(TagHelper.LOGIN)
            finish()
        }
    }

    @SuppressLint("HandlerLeak")
    val handler = object : Handler(){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (1 == msg.what){
                initView()
            }
        }
    }
}
